//////////////////////////
var filterFields = {};
var filterConds = {};
var filterValues = {};
$(document).ready(function(){
	$(".data_menubar_options").css("display", "none");
	$("span.ui-inplace-display").bind("click", function () { $(this).hide(); });
	//function(){alert($(this).attr("class"));$(this).addClass("ui-state-highlight");}
	$(".fields_select_cls option:first").attr("selected", true);
	
	$(".fields_select_cls option").each(function(){
		eval("filterFields.attr_"+$(this).val()+"='"+$(this).text()+"';");
	});
	$(".conds_select_cls:first option").each(function(){
		eval("filterConds.attr_"+$(this).val()+"='"+$(this).text()+"';");
	});
	//addEditFormStyle();
});

function toggleHL(_this) {
	 $(_this).toggleClass("ui-state-highlight");
}

function showInplaceContent(componentId) {
	var parent = $(componentId);
	parent.find(".ui-inplace-display").fadeOut("normal", function(){
		parent.find(".ui-inplace-content").fadeIn("normal");
	});
}

function hideInplaceContent(componentId, _this) {
	var parent = $(componentId);
	var inPlaceLabel = _this.find('option:selected').text();
	_this.parentsUntil(".data_menubar_options").find(".ui-inplace-display").text(inPlaceLabel);
	
	parent.find(".ui-inplace-content").fadeOut("normal", function(){
		parent.find(".ui-inplace-display").fadeIn("normal");
	});
}

function addEditFormStyle(componentId) {
	$(componentId).find(".edit_form_grid_row").each(function(index, item){
		$(this).find("td").each(function(colIdx, colItem){
			if (colIdx%2 == 0) {
				$(this).addClass("col_label");
			} else {
				$(this).addClass("col_field");
			}
		});
		$(this).find("td:last").addClass("last_grid_col");
	});
	$(componentId).find(".edit_form_grid .edit_form_grid_row:last").addClass("last_grid_row");
	var colspan = $(componentId).find(".edit_form_grid .edit_form_grid_row:first td").length
					- $(componentId).find(".edit_form_grid .edit_form_grid_row:last td").length;
	//alert("colspan:"+colspan);
	$(componentId).find(".edit_form_grid .edit_form_grid_row td:last").attr("colspan", colspan*2);
}
function editFormCallback(xhr, status, args, componentId) {  
	var parent = $(componentId);
    if(args.validationFailed || !args.success) {  
    	parent.find('.edit_dlg_cls').effect("shake", { times:3 }, 100);  
    } else {  
    	closeEditFormDlg(componentId);  
    }  
}
function updatedCallback(xhr, status, args, componentId) {  
    if(args.success) {  
    	showEditFormDlg(componentId);
    }
}
function showEditFormDlg(componentId) {
	var msgGrowl = eval($(componentId).attr("id")+"MsgGrowl");
	msgGrowl.removeAll();
	var editFormDlg = eval($(componentId).attr("id")+"EditDlg");
	addEditFormStyle(componentId);
	editFormDlg.show(); 
}
function closeEditFormDlg(componentId) {
//	var msgGrowl = eval($(componentId).attr("id")+"MsgGrowl");
//	alert($("#"+msgGrowl.id).find(".ui-growl-image").attr("class"));
////	msgGrowl.removeAll();
//	if (typeof msgGrowl.cfg.msgs != undefined) {
//		alert(msgGrowl.cfg.msgs.length);
//		for (var msg in msgGrowl.cfg.msgs) { 
//			alert(msg.severity);
//			msgGrowl.removeAll();
//			break;
//		}
//	}
	var editFormDlg = eval($(componentId).attr("id")+"EditDlg");
	editFormDlg.hide(); 
}
//function saveForm() {
//	//alert();
//	saveAccount([{name:"account_list_form-search_acc_name", value:"2"},
//		{name:"account_list_form-search_acc_nick", value:"ddd"}]);
//}
function changeIinplaceLabel(componentId, _this) {
	var inPlaceLabel = _this.find('option:selected').text();
	_this.parentsUntil(".data_menubar_options").find(".ui-inplace-display").text(inPlaceLabel);
	
	var varFilterCond = eval($(componentId).attr("id")+"FilterCond");
	varFilterCond.hide();
}

function setFilterValue(componentId) {
	var parent = $(componentId);
	var parentId = parent.attr("id");
	var valSelect = parent.find(".filter_value_cls select");

	if (valSelect.length > 0) {
		var _value = $(valSelect[0]).find("option:selected").text();
		var _otherValue = $(valSelect[1]).find("option:selected").text();
		parent.find(".hidden_op_val_cls").val(_value+"&"+_otherValue);
		
	} 
	return false;
}

function showFilterLabel(xhr, status, args, componentId) {
	
	var parent = $(componentId);
	var parentId = parent.attr("id");
	parent.find(".filter_item_label").each(function(){
		
		var filter = $(this).text().trim().split(";");
		var filterField = filter[0].trim();
		var filterCond = filter[1].trim();
		var filterValue = filter[2].trim();
		
		var _value = "";
		var _otherValue = "";
		//optionValue不为空，使用optionValue的值
		if (filterValue != "") {
			var values = filterValue.split("&");
			_value  = values[0].trim();
			_otherValue = values[1].trim();
			
		} 
		//查询全部时的条件显示 filter[1]为 condition
		//全部
		if (filterCond == 0) { 
			
			$(this).text(eval("filterFields.attr_all")+eval("filterConds.attr_5")+"["+_value+"]");
		//区间
		} else if (_otherValue != "" && _otherValue != "null") { 
			
			$(this).text(eval("filterFields.attr_"+filterField)+eval("filterConds.attr_"+filterCond)+"["+_value+"]~["+_otherValue+"]");
		//单值
		} else {
			$(this).text(eval("filterFields.attr_"+filterField)+eval("filterConds.attr_"+filterCond)+"["+_value+"]");
		}
	});
	
	if(typeof(args) == 'undefined' || !args.validationFailed) {  
		parent.find(".data_menubar_options").fadeOut(100, function(){
			parent.find(".label_menubar_options").fadeIn();
		});
	} 
}
function addSearch(componentId){
	var parent = $(componentId);
	parent.find(".label_menubar_options").fadeOut(100, function(){
		parent.find(".data_menubar_options").fadeIn();
		parent.find("#data_list_form-fields_select")
			.width(parent.find("#data_list_form-fields_select_input").width());
	});
}